package com.guan.ruiji.common;

import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.sql.SQLIntegrityConstraintViolationException;

/**
 * @author guanzhenjie
 */
@ControllerAdvice(annotations = {RestController.class})
@ResponseBody
public class GlobalExceptionHandler {

    @ExceptionHandler(SQLIntegrityConstraintViolationException.class)
    public R<String> exceptionHandler(SQLIntegrityConstraintViolationException ex){

        System.out.println(ex.getMessage());

        if(ex.getMessage().contains("Duplicate entry")){
            String[] str = ex.getMessage().split(" ");
            return R.error(str[2] + "已存在");
        }

        return R.error("未知错误");
    }
}
